<?php
namespace App\Services\Admin;

use App\Jobs\Fun;
use App\Repositories\Admin\CompanyRepository;
use App\Repositories\Admin\JobCompanyRepository;
use App\Repositories\Admin\JobStaffRepository;
use App\Repositories\Admin\StaffRepository;
use App\Repositories\Admin\UserRepository;

class JobStaffService
{
    private $service;
    private $user;
    private $jobCompany;
    private $company;
    private $staff;

    public function __construct()
    {
        $this->service = new JobStaffRepository();
        $this->user = new UserRepository();
        $this->jobCompany = new JobCompanyRepository();
        $this->company = new CompanyRepository();
        $this->staff = new StaffRepository();
    }

    public function getIndex()
    {
        $models = $this->service->getIndexList();
        if (!count($models)) { return ''; }
        foreach ($models as $model) {
            $company = $this->company->getShowOneByUid($model->cid);
            $user = $this->user->getShowOne($model->uid);
            $model->comName = $company ? $company->name : '测试公司';
            $model->userName = $user ? $user->name : '测试用户';
            $model->statusName = $this->service->getStatusName($model->status);
        }
        return $models;
    }

    /**
     * 根据公司UID，获取面试流程
     */
    public function getComIndexByCid($uid)
    {
        $models = $this->service->getIndexByCid($uid);
        if (!count($models)) { return ''; }
        foreach ($models as $model) {
            $user = $this->user->getShowOne($model->uid);
            $company = $this->jobCompany->getShowOne($model->cid);
            $model->uname = $user ? $user->name : '测试';
            $model->userHead = $user ? $user->head : '';
            $model->detail = ($company&&$company->detail) ?
                $company->detail : '测试描述';
            $model->statusName = $this->service->getStatusName($model->status);
        }
        return $models;
    }

    /**
     * APP端，企业
     * 根据岗位ID，获取面试详情
     */
    public function getComShowById($uid)
    {
        $model = $this->service->getShowOne($uid);
        $user = $this->user->getShowOne($uid);
        $staff = $this->staff->getShowOneByUid($uid);
        $dataArr = array();
        if (!$model) { return $dataArr; }
        $jobCom = $this->jobCompany->getAppShowOne($model->origin_id);
        return array(
            'id' => $model->id,
            'name' => $user->name,
            'typeName' => $model->from==1?'主动投递':'公司邀约',
            'statuss' => $model->getStatuss(),
            'status' => $model->status,
            'job' => $model->origin_id,
            'jobName' => $jobCom->jobName,
            'head' => $user->head,
            'image' => $staff->image,
            'sexName' => $user->sex==1?'男':'女',
            'eduName' => $this->staff->getEduNameByEdu($staff->education),
            'weight' => $staff->weight,
            'polity' => $this->staff->getPolityNameByPolity($staff->political_status),
            'nativePlace' => $staff->native_place,
            'nationality' => $staff->nationality,
            'marryName' => $this->staff->getMarryNameByMarry($staff->marriage),
            'house' => $staff->house,
            'idcard' => $staff->idcard,
            'householdAddress' => $staff->householdAddress,
            'contactPerson' => $staff->contactPerson,
            'skill' => $staff->skill,
            'hobby' => $staff->hobby,
        );
    }

    /**
     * APP端
     * 根据工人UID，获取面试流程
     */
    public function getStaffIndexByUid($uid)
    {
        $models = $this->service->getIndexByUid($uid);
        if (!count($models)) { return ''; }
        foreach ($models as $model) {
            $user = $this->user->getShowOne($model->uid);
            $com = $this->company->getShowOneByUid($model->uid);
            $jobCom = $this->jobCompany->getShowOne($model->cid);
            $model->cname = $com ? $com->com_name : '测试公司';
            $model->head =Fun::pImg($user ? $user->head : '');
            $model->detail = ($jobCom&&$jobCom->detail) ? $jobCom->detail : '测试描述';
            $model->statusName = $this->service->getStatusName($model->status);
        }
        return $models;
    }

    /**
     * APP端，工人
     * 根据岗位ID，获取面试详情
     */
    public function getJobShowById($uid)
    {
        $model = $this->service->getShowOne($uid);
        $dataArr = array();
        if (!$model) { return $dataArr; }
        $jobCom = $this->jobCompany->getAppShowOne($model->origin_id);
        return array(
            'id' => $model->id,
            'cname' => $jobCom->cname,
            'typeName' => $model->from==1?'主动投递':'公司邀约',
            'statuss' => $model->getStatuss(),
            'status' => $model->status,
            'job' => $model->origin_id,
            'jobName' => $jobCom->jobName,
            'head' => $jobCom->head,
            'cid' => $jobCom->cid,
            'sexName' => $jobCom->sexName,
            'contact' => $jobCom->contact,
            'telephone' => $jobCom->telphone,
            'address' => $jobCom->address,
            'email' => $jobCom->email,
            'eduName' => $jobCom->eduName,
            'emolument' => $jobCom->emolument,
            'welfare' => $jobCom->welfare,
            'rest' => $jobCom->rest,
            'workYear' => $jobCom->workYear,
            'workHour' => $jobCom->workHour,
            'ageRange' => $jobCom->ageRange,
            'detail' => $jobCom->detail,
            'skill' => $jobCom->skill,
            'content' => $jobCom->content,
        );
    }

    /**
     * 根据工人UID，设置状态
     */
    public function setStaffStatus($id,$status)
    {
        $model = $this->service->getShowOne($id);
        if (!$model) { return ''; }
        return $this->service->setAppStatus($id,$status);
    }

    /**
     * 获取面试评价
     */
    public function getComment($uid,$recruit)
    {
        return $this->service->getComment($uid,$recruit);
    }

    /**
     * 设置面试评价
     */
    public function setComment($data)
    {
        return $this->service->setComment($data);
    }

    public function add($data)
    {
        return $this->service->setAdd($data);
    }

    public function modify($data,$id)
    {
        return $this->service->setModify($data,$id);
    }

    public function getOne($id)
    {
        return $this->service->getShowOne($id);
    }
}